﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using project.WebServices;

/*
 * ==================================================================================
 * Author: Sneha Nagendra / Anusha K U
 * Created On: 03-03-2011 23:59  IST
 * This page accepts user information and creates a new account.
 * ==================================================================================
 */


namespace project.GUI
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        DropDownList RoleList = new DropDownList();
        DropDownList DepartmentList = new DropDownList();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void RoleList_DataBound(object sender, EventArgs e)
        {
            DropDownList RoleList = (DropDownList)cuw_Register.CreateUserStep.ContentTemplateContainer.FindControl("RoleList");
            //remove temp
            RoleList.Items.RemoveAt(0);
        }

        protected void cuw_Register_Error(object sender, CreateUserErrorEventArgs e)
        {
            Label lbl_Error = (Label)cuw_Register.CreateUserStep.ContentTemplateContainer.FindControl("lbl_Error");
            lbl_Error.Text = e.CreateUserError.ToString();
            lbl_Error.Visible = true;
            e.ToString();
        }

        protected void cuw_Register_Created(object sender, EventArgs e)
        {
            Label lbl_Error = (Label)cuw_Register.CreateUserStep.ContentTemplateContainer.FindControl("lbl_Error");
            lbl_Error.Visible = false;
            //continue with add new user
            LogService logAction = new LogService();
            DropDownList RoleList = (DropDownList)cuw_Register.CreateUserStep.ContentTemplateContainer.FindControl("RoleList");
            DropDownList DepartmentList = (DropDownList)cuw_Register.CreateUserStep.ContentTemplateContainer.FindControl("DepartmentList");
            TextBox txt_ConfirmPassword = (TextBox)cuw_Register.CreateUserStep.ContentTemplateContainer.FindControl("txt_ConfirmPassword");
            TextBox txt_Request = (TextBox)cuw_Register.CreateUserStep.ContentTemplateContainer.FindControl("txt_Request");
            String password = cuw_Register.Password;
            String confirmPass = txt_ConfirmPassword.Text;
            //need to add one after we remove temp
            int role = RoleList.SelectedIndex + 2;
            int department = DepartmentList.SelectedIndex + 1;
            String username = cuw_Register.UserName;
            String request = txt_Request.Text;
            String question = cuw_Register.Question;
            String answer = cuw_Register.Answer;
            String altEmail = cuw_Register.Email;

            RegisterService rs = new RegisterService();
            bool returnVal = rs.RegisterNewUser(username, password, confirmPass, request, role, department,altEmail,question,answer);
            //make user a temp role
            Roles.AddUserToRole(username, "Temp");

            if (returnVal)
            {
                logAction.LogAction(DateTime.Now.ToString() + ": A new user account has been created by the name of " + username + ".\n");
                Label lbl_Result = (Label)cuw_Register.CompleteStep.ContentTemplateContainer.FindControl("lbl_Result");
                lbl_Result.Text = ("The User is created " + username).ToString() + ". Please wait for the system admin to grant you access.";
                lbl_Result.Visible = true;

            }
            else
            {
                logAction.LogAction(DateTime.Now.ToString() + ": A new user account failed to be created due to an error in RegisterService.\n");
                Label lbl_Result = (Label)cuw_Register.CompleteStep.ContentTemplateContainer.FindControl("lbl_Result");
                lbl_Result.Text = ("The User is not created").ToString();
                lbl_Result.Visible = true;
            }

            Server.Transfer("~/GUI/Login.aspx");
        }

        protected void cuw_Register_Creating(object sender, LoginCancelEventArgs e)
        {
            Validate("CreateUserWizard");
        }

        protected void RoleList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
